package com.zzyy.dtx.order.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.zzyy.dtx.order.dto.OrderDTO;
import com.zzyy.dtx.order.entities.Order;
import com.zzyy.dtx.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther zzyy
 * @create 2021-04-17 22:02
 */
@RestController
@Slf4j
public class OrderController
{
    @Resource
    private OrderService orderService;

/*    @RequestMapping("/order/add")
    public String addOrder()
    {
        Order order = new Order();

        order.setUserId(1);
        order.setProductId(1);
        order.setPaymentMoney(new BigDecimal(100));
        order.setProductCount(1);
        order.setOrderNo(IdUtil.simpleUUID());

        order.setDecreaseScore(2);//扣除2个积分

        int i = orderService.add(order);

        return "-----success add order to mysql: "+"\t"+i+"\t"+DateUtil.now();
    }*/

    /**
     * couponNo请自己从tx_coupon_receive库选一张状态是1的，没用过的
     * 测试地址：http://localhost:8001/order/add?couponNo=2
     * @param couponNo
     * @return
     */
    @RequestMapping("/order/add")
    public String addOrder(@RequestParam("couponNo") Integer couponNo)
    {
        Order order = new Order();

        order.setUserId(1);
        order.setProductId(1);
        order.setPaymentMoney(new BigDecimal(100));
        order.setOrderNo(IdUtil.simpleUUID());

        order.setProductCount(1);//冻减1个产品
        order.setDecreaseScore(2);//扣除2个积分
        order.setCouponReceiveId(couponNo);//抵扣优惠卷,优惠券的码couponNo

        int i = orderService.add(order);

        return "-----success add order to mysql: "+"\t"+i+"\t"+DateUtil.now();
    }


    /**
     * 2023.4.16，查找所有的订单信息,提供给PayController类里面的getOrderList方法使用
     * @return
     */
    @RequestMapping("/order/orderList")
    public List<OrderDTO> orderList(){
        List<Order> orders= this.orderService.findAll();
        List<OrderDTO> orderList= new ArrayList<>();
        for(Order order:orders){
            OrderDTO obj=new OrderDTO();
            BeanUtils.copyProperties(order,obj);
            orderList.add(obj);
        }
        System.out.println("----进入订单orderList并返回了");
        return  orderList;
    }

    @RequestMapping(value = "/order/findOrderNo",method = RequestMethod.GET)
    public OrderDTO findOrderNo(@RequestParam("orderNo") String orderNo){
        Order order=this.orderService.findOrderNo(orderNo);

        OrderDTO orderDTO=new OrderDTO();
        BeanUtils.copyProperties(order,orderDTO);

        return orderDTO;
    }
}
